package com.better517na.elasticseach.context;

import com.alibaba.fastjson.JSON;
import com.better517na.elasticseach.elasticsearch.Index;
import com.better517na.elasticseach.handlers.CustomExportHandler;
import com.better517na.elasticseach.handlers.ExportDataHandler;
import com.better517na.elasticseach.handlers.Handler;
import com.better517na.elasticseach.handlers.ImportDataHandler;
import com.better517na.elasticseach.utils.StringUtil;

import java.util.Properties;

public class ElasticSearchContext {

    private String outPut;

    private String inPut;

    private String filePrefix;

    private int docCount;

    private Index producer;

    private Index consumer;

    private String esPort;

    private String esHost;

    private String exeMethod;

    private Custom custom;

    private Handler handler;

    public String getOutPut() {
        return outPut;
    }

    private ElasticSearchContext() {
    }

    public ElasticSearchContext(String outPut, String inPut, String filePrefix, int dicCount, Index producer, Index consumer, String esPort, String esHost, String exeMethod, Custom custom) {
        this.outPut = outPut;
        this.inPut = inPut;
        this.filePrefix = filePrefix;
        this.docCount = dicCount;
        this.producer = producer;
        this.consumer = consumer;
        this.esPort = esPort;
        this.esHost = esHost;
        this.exeMethod = exeMethod;
        this.custom = custom;
    }

    public Handler getHandler() {
        return handler;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public Custom getCustom() {
        return custom;
    }

    public String getInPut() {
        return inPut;
    }

    public String getFilePrefix() {
        return filePrefix;
    }

    public int getDocCount() {
        return docCount;
    }

    public Index getProducer() {
        return producer;
    }

    public Index getConsumer() {
        return consumer;
    }

    public String getEsPort() {
        return esPort;
    }

    public String getEsHost() {
        return esHost;
    }

    public String getExeMethod() {
        return exeMethod;
    }



    public static class ContextBuild{
        private Properties properties;

        public ContextBuild(Properties properties) {
            this.properties = properties;
        }

        public ElasticSearchContext build(InputParam inputParam){
            if (null == properties){
                throw new NullPointerException("初始化参数不能为空");
            }
            
            String outPut = properties.getProperty("file.out");
            String inPut = properties.getProperty("file.in");
            String prefix = properties.getProperty("file.prefix");
            String docCount = properties.getProperty("doc.count");
            String producerIndex = properties.getProperty("producer.index");
            String producerType = properties.getProperty("producer.type");
            String consumerIndex = properties.getProperty("consumer.index");
            String consumerType = properties.getProperty("consumer.type");
            String esHost = properties.getProperty("es.host");
            String esPort = properties.getProperty("es.port");
            String exeMethod = properties.getProperty("excute.method");
            String cusDsl = properties.getProperty("custom.dsl");
            //处理手动输入 覆盖配置文件
            if (null != inputParam){

                if (StringUtil.isNotEmpty(inputParam.getUtl())){
                    esHost = inputParam.getUtl();
                }

                if (StringUtil.isNotEmpty(inputParam.getPort())){
                    esPort = inputParam.getPort();
                }

                if (StringUtil.isNotEmpty(inputParam.getOutPut())){
                    outPut = inputParam.getOutPut();
                }

                if (StringUtil.isNotEmpty(inputParam.getInPut())){
                    inPut = inputParam.getInPut();
                }

                if (StringUtil.isNotEmpty(inputParam.getConsumerIndex())){
                    consumerIndex = inputParam.getConsumerIndex();
                }

                if (StringUtil.isNotEmpty(inputParam.getProducerIndex())){
                    producerIndex = inputParam.getProducerIndex();
                }

                if (StringUtil.isNotEmpty(inputParam.getConsumerType())){
                    consumerType = inputParam.getConsumerType();
                }

                if (StringUtil.isNotEmpty(inputParam.getProducerType())){
                    producerType = inputParam.getProducerType();
                }

                if (StringUtil.isNotEmpty(inputParam.getPrefix())){
                    prefix = inputParam.getPrefix();
                }

                if (StringUtil.isNotEmpty(inputParam.getExeMethod())){
                    exeMethod = inputParam.getExeMethod();
                }

                if (StringUtil.isNotEmpty(inputParam.getCusDsl())){
                    cusDsl = inputParam.getCusDsl();
                }
            }

            
            Index producer = new Index(producerIndex, producerType);
            Index consumer = new Index(consumerIndex, consumerType);
            Custom custom = new Custom();
            custom.setQueryDsl(cusDsl);

            ElasticSearchContext context = new ElasticSearchContext(outPut,inPut,prefix,Integer.parseInt(docCount),producer,consumer,esPort, esHost, exeMethod, custom);
            System.out.println("配置信息:"+JSON.toJSONString(context));
            Handler handler ;

            if ("export".equalsIgnoreCase(exeMethod)){
                handler = new ExportDataHandler();
            } else if ("custom".equalsIgnoreCase(exeMethod)){
                handler = new CustomExportHandler();
            }else {
                handler = new ImportDataHandler();
            }

            handler.setContext(context);
            context.setHandler(handler);
            return context;
        }
    }
}
